package com.pengyang.netty.demo.server;

import java.util.Map;

import com.pengyang.netty.demo.pojo.IMMessage;
import com.pengyang.netty.demo.pojo.MessageType;
import com.pengyang.netty.demo.pojo.SelfDefinedProtocol;
import com.pengyang.netty.demo.pojo.UserBindChannel;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

public class ServerHandler extends ChannelInboundHandlerAdapter {
	
	private ChannelHandlerContext ctx;
	
	@Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("服务端Handler创建。。。");
        super.handlerAdded(ctx);
    }
	
	@Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive:Channel未连接到远端");
        super.channelInactive(ctx);
    }
	
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.ctx = ctx;
        super.channelActive(ctx);
        System.out.println("有客户端连接：" + ctx.channel().remoteAddress().toString());
    }
	
	@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        IMMessage message = (IMMessage)msg;
        //认证消息
        if(message.getMsgType() == MessageType.TYPE_AUTH.value()){         
            System.out.println("认证消息：" + msg);
            UserBindChannel.add(message.getUid(),ctx);
        //text消息
        }else if(message.getMsgType() == MessageType.TYPE_TEXT.value()){    
        	Map<Integer, ChannelHandlerContext> onlineUsers = UserBindChannel.onlineUsers;
            ChannelHandlerContext c = UserBindChannel.getContext(message.getReceiveId());
            if(c==null){           //接收方不在线，反馈给客户端
                message.setMsg("对方不在线！");
                ctx.writeAndFlush(message);
            }else{                 //将消转发给接收方
                System.out.println("转发消息：" + msg);
                c.writeAndFlush(message);
            }
        //audio消息
        }else if (message.getMsgType() == MessageType.TYPE_AUDIO.value()) {
        	ChannelHandlerContext c = UserBindChannel.getContext(message.getReceiveId());
        	if(c==null){           //接收方不在线，反馈给客户端
                message.setMsg("对方不在线！");
                ctx.writeAndFlush(message);
            }else{                 //将消转发给接收方
                System.out.println("转发消息：" + msg);
                c.writeAndFlush(message);
            }
        //video消息	
		}else if (message.getMsgType() == MessageType.TYPE_VIDEO.value()) {
			ChannelHandlerContext c = UserBindChannel.getContext(message.getReceiveId());
			if(c==null){           //接收方不在线，反馈给客户端
                message.setMsg("对方不在线！");
                ctx.writeAndFlush(message);
            }else{                 //将消转发给接收方
                System.out.println("转发消息：" + msg);
                c.writeAndFlush(message);
            }
		}else if (message.getMsgType() == MessageType.TYPE_LOGOUT.value()) {
			UserBindChannel.remove(message.getUid());
		}
    }
	
	@Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("与客户端断开连接:"+cause.getMessage());
        cause.printStackTrace();
        ctx.close();
    }
    
}
